package com.jianzhi;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DownLoad {

	private static Connection con = null;
	private static PreparedStatement pst = null;
	private static ResultSet rs=null;
	static {
		try {
			Class.forName(Info.driver);// 指定连接类型
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		String string = "http://xa.jianzhi8.com/xuesheng/";
		URL url = null;
		try {
			url = new URL(string);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		String rep = "href=\"/a[^=]*\\.html\"";
		List<String> list = getList(getContent(string), rep);

		
		
		
		List<Info> infos = new ArrayList<>();

		for (int i = 0; i < list.size(); i++) {
			String string2 = url.getProtocol() + "://" + url.getHost()
					+ list.get(i).substring(list.get(i).indexOf('"') + 1, list.get(i).length() - 1);
			list.remove(i);
			list.add(i, string2);
		}
		

		for (String string2 : list) {
			string2 = getContent(string2);
			Info info = XmlParer.getInfo(string2);
			info.setTitle(info.getTitle().substring(0, info.getTitle().length() > 20 ? 20 : info.getTitle().length()));
			infos.add(info);
		}

		List<Info> infos2=findAll();
		
		for (Info info : infos) {
			boolean flag=true;
			for (Info info2 : infos2) {
				if(info.getTitle().equals(info2.getTitle()) && info.getDate().getTime()==info2.getDate().getTime()){
					flag=false;
				}
			}
			if(flag){
				addData(info);
			}
		}

	}

	/**
	 * 查询数据库的所有的记录
	 * 
	 * @return
	 */
	public static synchronized List<Info> findAll() {
		List<Info> infos=new ArrayList<>();
		

		String sql = "select title,date from job";

		try {
			con = DriverManager.getConnection(Info.url, Info.user, Info.pwd);
			pst = con.prepareStatement(sql);// 准备执行语句
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Info info=new Info();
				info.setTitle(rs.getString(1));
				Timestamp time=rs.getTimestamp(2);
				info.setDate(new Date(time.getTime()));
				infos.add(info);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(rs!=null){
					rs.close();
				}
				if (pst != null) {
					pst.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return infos;
	}

	
	/**
	 * 存储一个兼职信息
	 * @param info
	 */
	public static synchronized void addData(Info info) {

		Timestamp timestamp = new Timestamp(info.getDate().getTime());

		String images = "upload/default.jpg";
		String id = Tools.getId();
		String user_id = "20170224140239000002";
		int status = 1;

		String sql = "insert into job values(?,?,?,?,?,?,?)";

		try {
			con = DriverManager.getConnection(Info.url, Info.user, Info.pwd);
			pst = con.prepareStatement(sql);// 准备执行语句
			pst.setString(1, id);
			pst.setString(2,
					info.getTitle());
			pst.setString(3, info.getYaoqiu().substring(0,
					info.getYaoqiu().length() > 10000 ? 10000 : info.getYaoqiu().length()));
			pst.setString(4, images);
			pst.setTimestamp(5, timestamp);
			pst.setInt(6, status);
			pst.setString(7, user_id);

			pst.execute();

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (pst != null) {
					pst.close();
				}
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 根据正则表达式截取所有的兼职信息的连接
	 * 
	 * @param content
	 * @return
	 */
	public static List<String> getList(String content, String rep) {

		Pattern pattern = Pattern.compile(rep);
		Matcher matcher = pattern.matcher(content);
		List<String> list = new ArrayList<>();
		while (matcher.find()) {
			// 获得一个连接
			String str = matcher.group();
			list.add(str);
		}
		return list;
	}

	/**
	 * 根据网页的地址获得网页的内容
	 * 
	 * @param string
	 * @return
	 */
	public static String getContent(String string) {
		URL url = null;
		InputStream inputStream = null;

		ByteArrayOutputStream outputStream = null;
		try {
			url = new URL(string);
			inputStream = url.openStream();
			outputStream = new ByteArrayOutputStream();
			byte[] bs = new byte[1024];
			int len = 0;
			while ((len = inputStream.read(bs)) != -1) {
				outputStream.write(bs, 0, len);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (outputStream != null) {
					outputStream.close();
				}
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		String content = "";

		try {
			content = outputStream.toString("gbk");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return content;
	}

}
